package com.cyber.parkingremotev2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;

public class NetCommunication extends Communication {
	Socket mSocket;
	BufferedReader mIn;
	PrintWriter mOut;
	boolean mOk;
	final static int PORT = 60000;
	String mAddr = "";

	public String getName() {
		return mAddr;
	}

	public NetCommunication() {
		super();
		mOk = false;
	}

	@Override
	public ArrayList<String> listServers() {
		// FIXME
		ArrayList<String> list = new ArrayList<String>();
		return list;
	}

	@Override
	public void disconnectServer() {
		if (mOk) {
			try {
				mOk = false;
				mSocket.close();
				mAddr = "";
				sendMessage(ParkRemoteActivity.M_CONNECTION,
						ParkRemoteActivity.M_CONNECTION_INFO,
						getString(R.string.disconnected));
			} catch (IOException e) {
				sendMessage(ParkRemoteActivity.M_EXCEPTION,
						ParkRemoteActivity.M_EXCEPTION_INFO,
						e.toString() + e.getMessage());
			}
		}
	}

	@Override
	public void send(String content) {
		if (mOk) {
			mOut.print(content);
			mOut.flush();
		}

	}

	@Override
	public String recv() {
		if (mOk) {
			String data = "";
			try {
				data = mIn.readLine();
			} catch (IOException e) {
				sendMessage(ParkRemoteActivity.M_EXCEPTION,
						ParkRemoteActivity.M_EXCEPTION_INFO,
						e.toString() + e.getMessage());
			}
			return data;
		} else {
			return null;
		}
	}

	@Override
	protected void connectServer(String name) {
		try {
			InetAddress address = InetAddress.getByName(name);
			mSocket = new Socket(address, PORT);
			mIn = new BufferedReader(new InputStreamReader(
					mSocket.getInputStream()));
			mOut = new PrintWriter(new OutputStreamWriter(
					mSocket.getOutputStream()));
			mOk = true;
			mAddr = name;
			sendMessage(ParkRemoteActivity.M_CONNECTION,
					ParkRemoteActivity.M_CONNECTION_INFO,
					getString(R.string.connected));
		} catch (UnknownHostException e) {
			sendMessage(ParkRemoteActivity.M_CONNECTION,
					ParkRemoteActivity.M_CONNECTION_INFO,
					getString(R.string.disconnected));
			sendMessage(ParkRemoteActivity.M_EXCEPTION,
					ParkRemoteActivity.M_EXCEPTION_INFO,
					e.toString() + e.getMessage());

		} catch (IOException e) {
			sendMessage(ParkRemoteActivity.M_CONNECTION,
					ParkRemoteActivity.M_CONNECTION_INFO,
					getString(R.string.disconnected));
			sendMessage(ParkRemoteActivity.M_EXCEPTION,
					ParkRemoteActivity.M_EXCEPTION_INFO,
					e.toString() + e.getMessage());
		} catch (Exception e) {
			sendMessage(ParkRemoteActivity.M_CONNECTION,
					ParkRemoteActivity.M_CONNECTION_INFO,
					getString(R.string.disconnected));
			sendMessage(ParkRemoteActivity.M_EXCEPTION,
					ParkRemoteActivity.M_EXCEPTION_INFO,
					e.toString() + e.getMessage());
		}

	}

}
